const Service = require('egg').Service

class ArticleService extends Service {
	constructor(ctx) {
		super(ctx)
	}
	async index(params, username) {
		let { pageSize = 10, pageNum = 1 } = params
		pageSize = Number(pageSize)
		const offset = (pageNum - 1) * pageSize
		const user = await this.ctx.model.User.findOne({
			include: {
				model: this.ctx.model.UserInfo,
				attributes: ['role'],
			},
			where: {
				username,
			},
			distinct: true,
		})
		let data = null

		if (user.userInfo.role === 3) {
			data = await this.ctx.model.Article.findAndCountAll({
				include: [
					{
						model: this.ctx.model.User,
						attributes: ['username'],
					},
					{
						model: this.ctx.model.Tag,
						attributes: ['tagName'],
					},
				],
				limit: pageSize,
				offset,
				distinct: true,
			})
		} else {
			data = await this.ctx.model.Article.findAndCountAll({
				include: [
					{
						model: this.ctx.model.User,
						attributes: ['username'],
					},
					{
						model: this.ctx.model.Tag,
						attributes: ['tagName'],
					},
				],
				where: {
					authorId: user.id,
				},
				limit: pageSize,
				offset,
				distinct: true,
			})
		}

		return data
	}

	async create(body) {
		const { authorId, tagId } = body
		// 查询存在user tag
		const hasUser = await this.ctx.model.User.findOne({
			where: { id: authorId },
		})
		const hasTag = await this.ctx.model.Tag.findOne({
			where: { id: tagId },
		})
		if (hasUser == null) {
			return '不存在用户'
		}
		if (hasTag == null) {
			return '不存在文章类目'
		}
		await this.ctx.model.Article.create({
			...body,
		})
		return 'success'
	}

	async destroy(id) {
		const hasOne = await this.ctx.model.Article.findOne({
			where: { id },
		})
		if (hasOne) {
			await this.ctx.model.Article.destroy({
				where: { id },
			})
			return 'success'
		}
		return '不存在该数据'
	}

	async update(id, body) {
		const hasOne = await this.ctx.model.Article.findOne({
			where: { id },
		})
		const { tagId } = body
		if (hasOne) {
			const hasTag = await this.ctx.model.Tag.findOne({
				where: { id: tagId },
			})
			if (hasTag == null) {
				return '不存在文章类目'
			}
			await this.ctx.model.Article.update(
				{ ...body },
				{
					where: { id },
				}
			)
			return 'success'
		}
		return '不存在该数据'
	}

	async show(id) {
		const body = await this.ctx.model.Article.findOne({
			include: [
				{
					model: this.ctx.model.User,
					attributes: ['username'],
				},
			],
			distinct: true,
			where: { id },
		})
		if (body == null) {
			return {
				body,
				status: '不存在该数据',
			}
		} else {
			return {
				body,
				status: 'success',
			}
		}
	}

	async updateAudit(id, body) {
		let { status } = body // 1 提交审核 0撤销审核
        let _status = 0

        if(status == 1) {
            _status = 1
        }else if(status == 0) {
            _status = 0
        }

		await this.ctx.model.Article.update(
			{ status:_status },
			{
				where: { id },
			}
		)
		return 'success'
	}

	async updateHomePage(id, body, username) {
		let { status } = body // 1 首页展示 0撤销首页展示
		const user = await this.ctx.model.User.findOne({
			include: {
				model: this.ctx.model.UserInfo,
				attributes: ['role'],
			},
			where: {
				username,
			},
			distinct: true,
		})
		if (user.userInfo.role === 3) {
			await this.ctx.model.Article.update(
				{ showHome: status },
				{
					where: { id },
				}
			)
			return 'success'
		}
		return '暂无权限'
	}
}

module.exports = ArticleService
